package commonInfrastructure.accountManagement.managedbean;

import commonInfrastructure.accountManagement.entity.EmployeeEntity;
import commonInfrastructure.accountManagement.session.EmailSessionBean;
import commonInfrastructure.accountManagement.session.EmployeeSessionBean;

import commonInfrastructure.accountManagement.session.PasswordHashSessionBean;

import java.io.IOException;
import java.util.UUID;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

@ManagedBean
@ViewScoped
public class ResetPasswordManagedBean {

    @EJB
    private EmployeeSessionBean employeeSessionBean;
    @EJB
    private PasswordHashSessionBean passowordHashSessionBean;
    @EJB
    private EmailSessionBean emailSessionBean;
    private String userName;
    private String DOB;
    private EmployeeEntity employee;
    private String securityQuestion;
    private String ans;

    public ResetPasswordManagedBean() {
    }

    public void doVerify(ActionEvent event) throws IOException {
        employee = employeeSessionBean.getEmployeeByUserName(userName);
        if (employee == null) {
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Invalid UserName", ""));

        } else {

            if (employee.getDOB().equals(DOB) && employee.getSecurityQuestion().equals(securityQuestion) && employee.getAns().equals(ans)) {
                //FacesContext.getCurrentInstance().getExternalContext().getFlash().put("employee", employee);
                //FacesContext.getCurrentInstance().getExternalContext().redirect("resetPassword2.xhtml");
                //send email to private email
                String uuid = UUID.randomUUID().toString();
                String[] sArray = uuid.split("-");
                String initialPwd = sArray[0];
                employee.setPassword(initialPwd);
                employee.setPassword(passowordHashSessionBean.hashPassword(employee.getPassword()));
                employee.setFirstTimeLogin(true);
                employeeSessionBean.resetEmployee(employee);
                emailSessionBean.emailInitialPassward(employee.getPersonalEmail(), initialPwd); //send email
               // FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "New password has been sent to your personal email.", ""));
                  FacesContext.getCurrentInstance().getExternalContext().redirect("resetPasswordResult.xhtml");
            } else {
                FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Verification Failed.", ""));

            }
        }
    }

    public EmployeeSessionBean getEmployeeManagerSessionBeam() {
        return employeeSessionBean;
    }

    public void setEmployeeManagerSessionBeam(EmployeeSessionBean employeeManagerSessionBeam) {
        this.employeeSessionBean = employeeManagerSessionBeam;
    }

    public void retrieveEmployee() {
        employee = employeeSessionBean.getEmployeeByUserName(userName);
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getDOB() {
        return DOB;
    }

    public void setDOB(String DOB) {
        this.DOB = DOB;
    }

    public EmployeeEntity getEmployee() {
        return employee;
    }

    public void setEmployee(EmployeeEntity employee) {
        this.employee = employee;
    }

    public String getSecurityQuestion() {
        return securityQuestion;
    }

    public void setSecurityQuestion(String securityQuestion) {
        this.securityQuestion = securityQuestion;
    }

    public String getAns() {
        return ans;
    }

    public void setAns(String ans) {
        this.ans = ans;
    }
}
